/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import controller.UserPropertiesController;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import model.Base;
import model.Observable;
import model.Observer;
import model.Person;
import net.miginfocom.swing.MigLayout;

/**
 *
 * @author Ira
 */
public class UserPropertiesView extends JPanel implements Observer {
	
	//<editor-fold defaultstate="collapsed" desc="noms des composants">
	public static final String BTN_DELETE = "btn_delete";
	public static final String BTN_SAVE = "btn_save";
	//</editor-fold>
	
	private JLabel lblName = new JLabel("Nom");
	private JLabel lblFirstName = new JLabel("Prénom");
	private JLabel lblPseudo = new JLabel("Pseudo");
	private JLabel lblType = new JLabel("Type");
	
	private JTextField txtName = new JTextField(13);
	private JTextField txtFirstName = new JTextField(13);
	private JTextField txtPseudo = new JTextField(13);
	private JTextField txtType = new JTextField(13);
	
	private JButton btnSave = new JButton("Enregistrer");
	private JButton btnDelete = new JButton("Supprimer l'utilisateur");
	
	private Base model;
	private Person user = null;
	private View mainWindow;
	private UserPropertiesController controller;
	
	public UserPropertiesView(Base model, View mainWindow) {
		
		this.model = model;
		this.mainWindow = mainWindow;
		this.controller = new UserPropertiesController(model, this);
		
		TitledBorder border = BorderFactory.createTitledBorder("Informations sur l'utilisateur sélectionné");
		
		// Nom des composants
		this.btnDelete.setName(UserPropertiesView.BTN_DELETE);
		this.btnSave.setName(UserPropertiesView.BTN_SAVE);
		
		// Listeners
		this.btnDelete.addActionListener(this.controller);
		this.btnSave.addActionListener(this.controller);
		
		this.setLayout(new MigLayout());
		this.setBorder(border);
		this.setMinimumSize(new Dimension(440, 120));
		this.setPreferredSize(new Dimension(440, 120));
		this.setMaximumSize(new Dimension(440, 120));
		
		this.txtType.setEditable(false);
		
		this.add(this.lblName, "w 60!");
		this.add(this.txtName);
		this.add(this.lblFirstName, "w 60!");
		this.add(this.txtFirstName, "wrap");
		this.add(this.lblPseudo, "w 60!");
		this.add(this.txtPseudo);
		this.add(this.lblType, "w 60!");
		this.add(this.txtType, "wrap");
	}
	
	public void setContent(Person p) {
		this.user = p;
		this.txtName.setText(p.getName());
		this.txtFirstName.setText(p.getFirstName());
		this.txtPseudo.setText(p.getPseudo());
		this.txtType.setText(p.getType());
	}

	@Override
	public void update(Object items) {
		this.setContent((Person)items);
		
		this.remove(this.btnDelete);
		this.remove(this.btnSave);
		if(this.model.isUserModifiable((Person)items)) {
			this.btnDelete.setVisible(true);
			this.btnSave.setVisible(true);
			this.add(this.btnSave, "cell 0 2 4 1");
			this.add(this.btnDelete, "cell 0 2 4 1");
		}
		this.mainWindow.setUserPropertiesView();
	}
	
	public Person getUser() {
		return this.user;
	}

	/**
	 * @return the txtName
	 */
	public String getTxtName() {
		return txtName.getText();
	}

	/**
	 * @return the txtFirstName
	 */
	public String getTxtFirstName() {
		return txtFirstName.getText();
	}

	/**
	 * @return the txtPseudo
	 */
	public String getTxtPseudo() {
		return txtPseudo.getText();
	}

	/**
	 * @return the txtType
	 */
	public String getTxtType() {
		return txtType.getText();
	}
}
